/* =====================================================================
 * Mobile / touch layout for Tentacle Realm Escape
 * ---------------------------------------------------------------------
 * 🦆 DUCK EXPLANATION
 * The <head> of index.html puts classes on <html>:
 *   .touch    -> the device has a touch screen (phone or tablet)
 *   .m        -> touch AND small screen: use the phone layout below
 *   .portrait -> the screen is taller than it is wide
 * Every rule in this file starts with one of those classes, so on a
 * normal PC with a mouse NONE of this applies and the game looks the
 * same as before.
 * ===================================================================== */

/* ---------- Shared touch fixes (phones AND tablets) ---------- */
html.touch, html.touch body {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    overscroll-behavior: none;
    touch-action: manipulation;          /* no double-tap zoom delay */
}
html.touch body { height: 100dvh; width: 100vw; }
html.touch .grid-cell,
html.touch .slot,
html.touch .char-card,
html.touch button { -webkit-user-select: none; user-select: none; }

/* Hover effects "stick" after a tap on touch screens: neutralise them. */
@media (hover: none) {
    #menu-main > button:hover { transform: none; }
    .grid-cell:hover { background: inherit; }
}

/* ---------- Rotate-your-phone notice ---------- */
#m-rotate {
    display: none;
    position: fixed; inset: 0; z-index: 100000;
    background: #0f0b15; color: #e2e8f0;
    flex-direction: column; align-items: center; justify-content: center;
    gap: 14px; text-align: center; padding: 24px;
    font-family: 'Roboto', 'Noto Sans JP', sans-serif;
}
#m-rotate .m-rotate-icon { font-size: 56px; animation: m-rot 1.8s ease-in-out infinite; }
#m-rotate .m-rotate-en { font-size: 18px; font-weight: bold; }
#m-rotate .m-rotate-jp { font-size: 15px; color: #94a3b8; }
@keyframes m-rot { 0%,20% { transform: rotate(0); } 60%,100% { transform: rotate(-90deg); } }
html.m.portrait #m-rotate { display: flex; }

/* =====================================================================
 * PHONE LAYOUT (html.m)
 * ===================================================================== */

html.m { font-size: 13px; }
html.m body { height: calc(var(--vh, 1vh) * 100); }

/* ---- Age gate ---- */
html.m .age-overlay { overflow-y: auto; align-items: flex-start; padding: 8px 0; }
html.m .age-overlay > * { margin: auto; transform: scale(0.9); transform-origin: top center; }

/* ---- Title / main menu ---- */
html.m #title-display { top: 10px; left: 14px; font-size: 1.9rem; letter-spacing: 1px; }
html.m .lang-select { top: 52px; left: 16px; }
html.m .lang-select button { padding: 6px 12px; }
html.m #menu-main {
    right: 3%; width: min(46vw, 330px); gap: 8px;
    transform: translateY(-50%);        /* drop the 3D tilt: it steals width */
}
html.m #menu-main > button { height: auto; min-height: 50px; padding: 8px 18px; }
html.m #menu-main > button .btn-title { font-size: 1.25rem; letter-spacing: 2px; }
html.m #menu-main > button .btn-subtitle { font-size: 0.7rem; margin-top: 2px; }

/* Discord / advert box: keep it, but small and in the corner. */
html.m .ad-module { width: 150px; left: 10px; bottom: 10px; }
html.m .ad-module .ad-caption { font-size: 10px; padding: 4px 6px; }
html.m .ad-nav-btn { width: 26px; height: 26px; }

/* ---- Secondary menus (level select, mission setup, gallery, team) ---- */
html.m .menu-container,
html.m .menu-container-wide {
    max-height: calc(var(--vh, 1vh) * 100 - 12px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 18px;
    gap: 8px;
}
html.m .menu-container { max-width: min(600px, 92vw); }
html.m .menu-container h3 { margin: 4px 0; }
html.m .level-grid { gap: 8px; }
html.m .btn-level { width: 52px; height: 44px; }
html.m .config-section { gap: 4px; width: 100%; }
html.m .config-select { padding: 8px; font-size: 14px; }
html.m .config-actions { gap: 8px; margin-top: 4px; }
html.m .btn-primary { padding: 10px 18px; }

/* Any modal/overlay content must scroll instead of being cut off. */
html.m .modal,
html.m .settings-content,
html.m .tutorial-modal {
    max-height: calc(var(--vh, 1vh) * 100 - 16px);
    overflow-y: auto;
}

/* Title block: title + subtitle, then language buttons, then the NSFW note */
html.m #title-display { top: 24px; max-width: 48vw; line-height: 1.1; }
html.m #title-display > div { font-size: 0.8rem !important; margin-top: 4px !important; }
html.m .lang-select { top: 104px; }
html.m #loading-screen .note {
    position: absolute; top: 4px; left: 16px; margin: 0;
    font-size: 10px; opacity: 0.7;
}

/* ===================== STEP 4: battle screen layout ===================== */
/* The three columns stay side by side (the old 900px rule stacked them). */
html.m .layout {
    flex-direction: row !important;
    height: calc(var(--vh, 1vh) * 100);
    position: relative;
}
/* Log column hidden by default; it becomes a slide-out drawer (step 5). */
html.m .left-panel { display: none; }

/* Map column takes everything that is left */
html.m .map-section { flex: 1 1 auto; min-width: 0; height: 100%; }

/* Compact top bar: one thin row, the button strip scrolls sideways */
html.m .top-bar {
    height: 34px; padding: 0 6px; gap: 6px;
    justify-content: flex-start;
}
html.m .top-bar .stats {
    font-size: 11px; white-space: nowrap; flex-shrink: 0;
    display: flex; align-items: center; gap: 3px;
}
html.m .top-bar .stats .btn-sm { padding: 2px 6px; font-size: 10px; }
html.m .top-controls {
    gap: 4px; overflow-x: auto; overflow-y: hidden; flex: 1 1 auto; min-width: 0;
    justify-content: flex-end; scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
html.m .top-controls::-webkit-scrollbar { display: none; }
html.m .top-controls .btn-sm,
html.m #btn-build { padding: 3px 7px !important; font-size: 10px; white-space: nowrap; flex-shrink: 0; min-height: 26px; }

html.m .global-buff-container { top: 36px; padding: 2px 6px; gap: 4px; }
html.m .global-buff-item { font-size: 10px; padding: 2px 5px; }

/* The map window fills the column (inline px sizes from updateMapZoom are overridden) */
html.m #map-container.map-container {
    width: auto !important; height: auto !important;
    max-width: none !important; max-height: none !important;
    align-self: stretch; flex: 1 1 auto;
    margin: 0 !important; border-width: 2px;
    transition: none;
}

/* Right panel: docked, narrower, and collapsible */
html.m .control-panel {
    width: min(300px, 38vw) !important; height: 100%;
    overflow-y: auto; overflow-x: hidden;
    transition: width 0.2s ease, margin 0.2s ease;
    -webkit-overflow-scrolling: touch;
}
html.m.m-panel-closed .control-panel { width: 0 !important; border-left: none; }
html.m #unit-panel { max-height: none; }
html.m #squad-list { max-height: none; }
html.m #large-portrait-container { display: none; }   /* big art is too heavy for a phone column */

/* The tab that opens/closes the right panel */
#m-panel-toggle { display: none; }
html.m.m-in-battle #m-panel-toggle {
    display: flex; align-items: center; justify-content: center;
    position: fixed; top: 50%; transform: translateY(-50%);
    right: min(300px, 38vw); z-index: 150;
    width: 22px; height: 64px; padding: 0;
    background: rgba(88, 28, 135, 0.9); color: #fff;
    border: 1px solid #a855f7; border-right: none; border-radius: 8px 0 0 8px;
    font-size: 14px; transition: right 0.2s ease;
}
html.m.m-in-battle.m-panel-closed #m-panel-toggle { right: 0; }

/* Squeeze the unit panel contents a little */
html.m .control-panel .panel-section { padding: 6px 8px; }
html.m .control-panel h3, html.m .control-panel h4 { font-size: 0.85rem; margin: 2px 0; }

/* ===================== STEP 5: End Turn + battle log ===================== */
#m-end-turn, #m-log-toggle, #m-log-shade { display: none; }

html.m.m-in-battle #m-end-turn {
    display: block; position: fixed; z-index: 150;
    left: 10px; bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    min-width: 110px; height: 44px; padding: 0 14px;
    font-size: 14px; font-weight: bold; letter-spacing: 1px;
    color: #fff; background: linear-gradient(180deg, #9333ea, #6b21a8);
    border: 2px solid #d8b4fe; border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.6);
}
html.m.m-in-battle #m-end-turn:disabled { opacity: 0.4; filter: grayscale(0.6); }

html.m.m-in-battle #m-log-toggle {
    display: block; position: fixed; z-index: 150;
    left: 128px; bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    height: 44px; padding: 0 10px; font-size: 12px;
    color: #e9d5ff; background: rgba(24, 24, 27, 0.9);
    border: 1px solid #7c3aed; border-radius: 10px;
}

/* The original log column slides in from the left as a drawer */
html.m.m-in-battle.m-log-open .left-panel {
    display: flex; position: fixed; left: 0; top: 0; bottom: 0;
    width: min(320px, 70vw); z-index: 170;
    box-shadow: 4px 0 20px rgba(0,0,0,0.7);
}
html.m .left-panel .left-action-area { display: none; }  /* End Turn lives in the floating button */
html.m .left-panel .log-container { flex: 1; overflow-y: auto; font-size: 12px; -webkit-overflow-scrolling: touch; }
html.m.m-in-battle.m-log-open #m-log-shade {
    display: block; position: fixed; inset: 0; z-index: 165; background: rgba(0,0,0,0.45);
}
html.m.m-log-open #m-log-toggle { z-index: 175; left: calc(min(320px, 70vw) + 10px); }
html.m.m-in-battle.m-log-open .left-panel { height: auto !important; border-bottom: none; border-right: 1px solid var(--border-color); }
html.m.m-log-open #m-end-turn { display: none !important; }

/* ===================== STEP 6: map zoom ===================== */
html.m #btn-zoom-toggle { display: none; }          /* replaced by the + / − bar */
html.m #map-container { touch-action: pan-x pan-y; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
#m-zoom { display: none; }
html.m #m-zoom {
    display: flex; align-items: center; gap: 2px;
    position: absolute; right: 30px; bottom: 10px; z-index: 60;
    background: rgba(24, 24, 27, 0.85); border: 1px solid #7c3aed; border-radius: 10px; padding: 2px;
}
html.m #m-zoom button {
    width: 40px; height: 38px; font-size: 22px; line-height: 1; padding: 0;
    color: #fff; background: transparent; border: none;
}
html.m #m-zoom span { min-width: 42px; text-align: center; font-size: 11px; color: #d8b4fe; }

/* ===================== STEP 7: touch controls ===================== */
#m-cancel { display: none; }
html.m #m-cancel.show {
    display: block; position: absolute; z-index: 60;
    right: 30px; bottom: 58px; height: 40px; padding: 0 14px;
    font-size: 13px; font-weight: bold; color: #fff;
    background: rgba(153, 27, 27, 0.92); border: 1px solid #f87171; border-radius: 10px;
}
html.m .qte-btn { min-width: 64px; min-height: 64px; font-size: 22px; touch-action: none; }
html.m .qte-btn.m-pressed { transform: scale(0.92); filter: brightness(1.4); }
html.touch .slot { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
html.m #ui-tooltip { max-width: 70vw; max-height: 80vh; overflow-y: auto; font-size: 12px; }

/* ===================== STEP 8: team menu ===================== */
#m-toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
    z-index: 9999; padding: 8px 16px; border-radius: 20px;
    background: rgba(24, 24, 27, 0.95); color: #f5f3ff; border: 1px solid #7c3aed;
    font-size: 13px; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
}
#m-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
html.touch .char-card { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
html.touch .char-card img { pointer-events: none; }
/* the ad window only belongs on the main menu */
html.m #loading-screen:has(#menu-main.hidden) #menu-advertise { display: none; }

html.m #menu-team {
    height: calc(var(--vh, 1vh) * 100 - 12px);
    width: min(820px, 96vw); max-width: 96vw;
    display: flex; flex-direction: column; overflow: hidden;
    padding: 8px 12px;
}
html.m #menu-team:not(.hidden) { display: flex; }
html.m #menu-team.hidden { display: none; }
html.m #menu-team h3 { margin: 0 0 4px; font-size: 1rem; }
html.m .team-layout { gap: 8px; flex: 1 1 auto; min-height: 0; }
html.m .team-panel { padding: 6px 8px; overflow: hidden; }
html.m .team-panel h4 { font-size: 0.75rem; margin: 0 0 6px; padding-bottom: 4px; }
html.m .roster-grid { grid-template-columns: repeat(4, 1fr); grid-auto-rows: 70px; gap: 6px; -webkit-overflow-scrolling: touch; }
html.m .squad-grid {
    grid-template-rows: none; grid-auto-rows: 62px; gap: 6px;
    height: auto; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
html.m .char-card img { width: 36px; height: 36px; margin-bottom: 2px; }
html.m .char-card span { font-size: 0.65rem; }
html.m .detail-panel { overflow-y: auto; gap: 8px; padding: 8px; -webkit-overflow-scrolling: touch; }
html.m #btn-back-main-team { margin-top: 6px; padding: 6px 18px; flex-shrink: 0; }
html.m .skills-grid-6 { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: none; }
html.m .skills-grid-6 > * { min-width: 0; overflow: hidden; }
html.m .detail-panel * { max-width: 100%; }

/* ===================== STEP 9: story + other screens ===================== */
html.m .story-textbox-container {
    height: auto; min-height: 32%;
    padding: 28px 7% calc(10px + env(safe-area-inset-bottom, 0px));
}
html.m .story-text.en { font-size: 14px; line-height: 1.35; }
html.m .story-text.jp { font-size: 13px; margin-top: 4px; }
html.m .story-name { font-size: 14px; }
html.m .story-top-buttons .story-btn { padding: 8px 12px; font-size: 13px; }
html.m .story-choices-container button,
html.m .story-choice { padding: 10px 16px; font-size: 14px; min-height: 44px; }
html.m .settings-content { width: min(420px, 90vw); padding: 14px 18px; }
html.m .volume-slider { height: 28px; }
html.m .overlay-content { max-width: 90vw; padding: 16px; }
html.m .tutorial-content { max-width: 92vw; max-height: 86vh; overflow-y: auto; font-size: 13px; }
html.m .boss-title { font-size: 2.2rem; }
html.m .overlay-text { font-size: 2rem; }
html.m #settings-overlay { position: fixed !important; inset: 0; z-index: 3000 !important; }
html.m .settings-content { max-height: 92vh; overflow-y: auto; padding: 10px 18px; }
html.m .settings-title { margin: 0 0 6px; font-size: 20px; }
html.m .settings-content .volume-slider { height: 22px; }
html.m .settings-content { gap: 8px !important; } html.m .settings-content > * { flex-shrink: 0; } html.m .setting-item { gap: 4px; } html.m .settings-footer { margin-top: 4px; } html.m .lang-btn { padding: 8px; }
/* hide floating phone buttons while a blocking overlay is open */
html.m body:has(#settings-overlay:not(.hidden)) #m-end-turn,
html.m body:has(#settings-overlay:not(.hidden)) #m-log-toggle,
html.m body:has(#settings-overlay:not(.hidden)) #m-panel-toggle,
html.m body:has(#settings-overlay:not(.hidden)) #m-zoom,
html.m body:has(#overlay-end:not(.hidden)) #m-end-turn,
html.m body:has(#overlay-end:not(.hidden)) #m-log-toggle,
html.m body:has(#overlay-end:not(.hidden)) #m-zoom { display: none !important; }
html.m body:has(#qte-overlay:not(.hidden)) #m-end-turn,
html.m body:has(#qte-overlay:not(.hidden)) #m-log-toggle,
html.m body:has(#tutorial-overlay:not(.hidden)) #m-end-turn,
html.m body:has(#tutorial-overlay:not(.hidden)) #m-log-toggle { display: none !important; }
html.m #overlay-end, html.m #qte-overlay, html.m #tutorial-overlay { position: fixed !important; inset: 0; z-index: 2500 !important; }
html.m body:has(#overlay-end:not(.hidden)) #m-panel-toggle { display: none !important; }

/* ===================== Layout switch button (PC <-> Mobile) =====================
 * Not scoped to html.m: it must show in the PC layout too. */
.layout-switch-btn {
    font-family: inherit; font-size: 13px; color: #e8dcff;
    background: rgba(40, 20, 70, 0.85); border: 1px solid #8b5cf6; border-radius: 8px;
    padding: 8px 14px; cursor: pointer;
}
.layout-switch-btn:hover { background: rgba(90, 50, 150, 0.95); }
#layout-switch-menu { position: fixed; right: 16px; bottom: 16px; z-index: 50; }
html.m #layout-switch-menu { right: 10px; bottom: calc(10px + env(safe-area-inset-bottom, 0px)); padding: 8px 12px; }
.layout-switch-row .layout-switch-btn { width: 100%; }
#layout-switch-rotate { margin-top: 24px; }
#layout-switch-float {
    position: fixed; left: 50%; top: 6px; transform: translateX(-50%);
    z-index: 99999; font-size: 18px; padding: 4px 12px; opacity: 0.85;
}
